package com.myk.game.gobangchess.expand.bwantispace;

import com.myk.game.gobangchess.signs.BadgeSign;
import com.myk.game.gobangchess.signs.ColorSign;
import com.myk.game.gobangchess.signs.TextSign;

import java.util.ArrayList;
import java.util.List;

/**
 * 棋子-主角
 */
public class Human extends BasePiece implements IGameInformation {

    protected int hp; //生命值
    protected int atk; //攻击力
    protected int sp; //移动距离

    public Human(Game game, int row, int col) {
        super(game, "你", row, col);
        this.hp = 50;
        this.atk = 5;
        this.sp = 2;
    }

    @Override
    public String getInformation() {
        return "\t\t【主角】\n" +
                "生命值：" + hp + "\n" +
                "攻击力：" + atk + "\n" +
                "移动距离：" + sp + "\n";
    }

    @Override
    public void renderSelf() {
        //绘制棋子底座
        game.helper.room.curGrids[row][col] = 1;
        //绘制名称
        game.helper.room.signList.add(new TextSign(game.helper.room.getIndex(row, col), "你", GameValue.COLOR_WHITE));
        //绘制生命值
        game.helper.room.signList.add(new BadgeSign(game.helper.room.getIndex(row, col), hp + "", 0, GameValue.COLOR_BLUE, GameValue.COLOR_TRANSPARENT));
        //绘制攻击值
        game.helper.room.signList.add(new BadgeSign(game.helper.room.getIndex(row, col), atk + "", 2, GameValue.COLOR_RED, GameValue.COLOR_TRANSPARENT));

        //当前处于玩家行动时
        if (game.helper.room.getCurPlayer() == game.helper.room.manager.getHumanPlayer()) {
            //绘制移动路径
            for (Integer index : getMoveRang()) {
                game.helper.room.signList.add(new ColorSign(index, GameValue.COLOR_GREEN_LIGHT));
            }
            //绘制攻击路径
            for (Integer index : getAttackRang()) {
                game.helper.room.signList.add(new ColorSign(index, GameValue.COLOR_YELLOW_LIGHT));
            }
        }
    }

    public boolean moveOrAttack(int row, int col) {
        //移动判断
        if (getMoveRang().contains(game.helper.room.getIndex(row, col))) {
            game.helper.room.curGrids[this.row][this.col] = 0;
            this.row = row;
            this.col = col;
            game.helper.room.curGrids[this.row][this.col] = 1;
            return true;
        }
        //攻击判断
        BaseMonsterPiece monster = game.helper.getMonsterByPos(row, col);
        if (monster != null && getAttackRang().contains(game.helper.room.getIndex(row, col))) {
            monster.hp -= this.atk;
            if (monster.hp <= 0) {
                game.fieldMonsterList.remove(monster);
                game.killCount++;
            }
            return true;
        }
        return false;
    }

    public List<Integer> getMoveRang() {
        List<Integer> moveRang = new ArrayList<>();
        List<int[]> posList = game.helper.room.manager.getCanMovedPos(this.row, this.col, this.sp);
        for (int[] pos : posList) {
            moveRang.add(game.helper.room.getIndex(pos[0], pos[1]));
        }
        return moveRang;
    }

    //获取攻击范围
    public List<Integer> getAttackRang() {
        List<Integer> attackRang = new ArrayList<>();
        List<int[]> posList = new ArrayList<>();
        posList.add(new int[]{row, col - 1});
        posList.add(new int[]{row, col + 1});
        posList.add(new int[]{row - 1, col});
        posList.add(new int[]{row + 1, col});
        posList.add(new int[]{row - 1, col - 1});
        posList.add(new int[]{row - 1, col + 1});
        posList.add(new int[]{row + 1, col - 1});
        posList.add(new int[]{row + 1, col + 1});
        posList.removeIf(pos -> !game.helper.room.manager.isInGrids(pos[0], pos[1]));
        posList.removeIf(pos -> game.helper.room.curGrids[pos[0]][pos[1]] != 7);
        for (int[] pos : posList) {
            attackRang.add(game.helper.room.getIndex(pos[0], pos[1]));
        }
        return attackRang;
    }
}